<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="../Connections/A146240Connection.jsp" %>
<%
// *** Edit Operations: declare variables

// set the form action variable
String MM_editAction = request.getRequestURI();
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
  String queryString = request.getQueryString();
  String tempStr = "";
  for (int i=0; i < queryString.length(); i++) {
    if (queryString.charAt(i) == '<') tempStr = tempStr + "&lt;";
    else if (queryString.charAt(i) == '>') tempStr = tempStr + "&gt;";
    else if (queryString.charAt(i) == '"') tempStr = tempStr +  "&quot;";
    else tempStr = tempStr + queryString.charAt(i);
  }
  MM_editAction += "?" + tempStr;
}

// connection information
String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null;

// redirect information
String MM_editRedirectUrl = null;

// query string to execute
StringBuffer MM_editQuery = null;

// boolean to abort record edit
boolean MM_abortEdit = false;

// table information
String MM_editTable = null, MM_editColumn = null, MM_recordId = null;

// form field information
String[] MM_fields = null, MM_columns = null;
%>
<%
// *** Insert Record: set variables

if (request.getParameter("MM_insert") != null && request.getParameter("MM_insert").toString().equals("form1")) {

  MM_editDriver     = MM_A146240Connection_DRIVER;
  MM_editConnection = MM_A146240Connection_STRING;
  MM_editUserName   = MM_A146240Connection_USERNAME;
  MM_editPassword   = MM_A146240Connection_PASSWORD;
  MM_editTable  = "A152786.USER_STORE";
  MM_editRedirectUrl = "index.jsp";
  String MM_fieldsStr = "USER_NAME|value|USER_NAME|value|USER_PASSWORD|value|USER_CONTACTNO|value|USER_EMAIL|value";
  String MM_columnsStr = "USER_ID|',none,''|USER_NAME|',none,''|USER_PASSWORD|',none,''|USER_CONTACTNO|none,none,NULL|USER_EMAIL|',none,''";

  // create the MM_fields and MM_columns arrays
  java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_fieldsStr,"|");
  MM_fields = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken();

  tokens = new java.util.StringTokenizer(MM_columnsStr,"|");
  MM_columns = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken();

  // set the form values
  for (int i=0; i+1 < MM_fields.length; i+=2) {
    MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):"");
  }
 
  // append the query string to the redirect URL
  if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) {
    MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString();
  }
}
%>
<%
// *** Insert Record: construct a sql insert statement and execute it

if (request.getParameter("MM_insert") != null) {

  // create the insert sql statement
  StringBuffer MM_tableValues = new StringBuffer(), MM_dbValues = new StringBuffer();
  for (int i=0; i+1 < MM_fields.length; i+=2) {
    String formVal = MM_fields[i+1];
    String elem;
    java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_columns[i+1],",");
    String delim    = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    String altVal   = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    if (formVal.length() == 0) {
      formVal = emptyVal;
    } else {
      if (altVal.length() != 0) {
        formVal = altVal;
      } else if (delim.compareTo("'") == 0) {  // escape quotes
        StringBuffer escQuotes = new StringBuffer(formVal);
        for (int j=0; j < escQuotes.length(); j++)
          if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
        formVal = "'" + escQuotes + "'";
      } else {
        formVal = delim + formVal + delim;
      }
    }
    MM_tableValues.append((i!=0)?",":"").append(MM_columns[i]);
    MM_dbValues.append((i!=0)?",":"").append(formVal);
  }
  MM_editQuery = new StringBuffer("insert into " + MM_editTable);
  MM_editQuery.append(" (").append(MM_tableValues.toString()).append(") values (");
  MM_editQuery.append(MM_dbValues.toString()).append(")");
  
  if (!MM_abortEdit) {
    // finish the sql and execute it
    Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance();
    Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);
    PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString());
    MM_editStatement.executeUpdate();
    MM_connection.close();

    // redirect with URL parameters
    if (MM_editRedirectUrl.length() != 0) {
      response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl));
      return;
    }
  }
}
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<%
Driver DrivergetUserNameList = (Driver)Class.forName(MM_A146240Connection_DRIVER).newInstance();
Connection ConngetUserNameList = DriverManager.getConnection(MM_A146240Connection_STRING,MM_A146240Connection_USERNAME,MM_A146240Connection_PASSWORD);
PreparedStatement StatementgetUserNameList = ConngetUserNameList.prepareStatement("SELECT USER_NAME FROM A152786.USER_STORE ORDER BY USER_NAME ASC");
ResultSet getUserNameList = StatementgetUserNameList.executeQuery();
boolean getUserNameList_isEmpty = !getUserNameList.next();
boolean getUserNameList_hasData = !getUserNameList_isEmpty;
Object getUserNameList_data;
int getUserNameList_numRows = 0;
int Repeat1__numRows = -1;
int Repeat1__index = 0;
int count=0;
getUserNameList_numRows += Repeat1__numRows;
String[] nameList = new String[1];
%>
<script LANGUAGE="JavaScript" type="text/javascript">	
<%out.println("var userArray = new Array();");	
while ((getUserNameList_hasData)&&(Repeat1__numRows-- != 0)) {
	out.println("userArray["+count+"]=\""+(((getUserNameList_data = getUserNameList.getObject("USER_NAME"))==null || getUserNameList.wasNull())?"":getUserNameList_data)+"\";");
	count++;
	Repeat1__index++;
	getUserNameList_hasData = getUserNameList.next();
}
getUserNameList.close();
StatementgetUserNameList.close();
ConngetUserNameList.close();
%>
</script>
<script>
var thefunction=function checkName(){
	var input = document.form1.USER_NAME.value;
	var check = false;
	if(input.length>5){
		for (i=0;i<=userArray.length;i++){
			if(userArray[i]==input){ 
				check=true;
			}
			if(!check){
				document.getElementById("checkingName").innerHTML="<img src=\"../images/right.png\" width=\"30\" height=\"30\"><span class=\"style4\"> USER NAME available.</span> ";
			}else{
				document.getElementById("checkingName").innerHTML="<img src=\"../images/wrong.png\" width=\"30\" height=\"30\"><span class=\"style4\"> USER NAME not available.</span> ";
			}
		}
	}
}
</script>
<style type="text/css">
<!--
.style1 {
	font-size: x-large;
	font-weight: bold;
}
.style4 {font-size: x-small}
-->
</style>
</head>

<body>
<form method="post" action="<%=MM_editAction%>" name="form1">
  <table width="525" align="center">
    <tr valign="baseline">
      <td colspan="3" align="right" nowrap><div align="left" class="style1">
        <p align="center">SIGN UP</p>
        </div></td>
    </tr>
    <tr valign="middle">
      <td width="149" align="right" nowrap><div align="right">User Name:</div></td>
      <td width="192"><input type="text" name="USER_NAME" value="" size="32" onBlur = "thefunction()">
      </td>
      <td width="168" height="35"><div id="checkingName"><img src="../images/wrong.png" width="30" height="30"><span class="style4"> USER NAME not available.</span></div></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap class="style4">&nbsp;</td>
      <td colspan="2" class="style4">*must be more than 5 character </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="right">User Password:</div></td>
      <td colspan="2"><input type="password" name="USER_PASSWORD" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="right">User Contact No:</div></td>
      <td colspan="2"><input type="text" name="USER_CONTACTNO" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="right">User Email:</div></td>
      <td colspan="2"><input type="text" name="USER_EMAIL" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td height="26" align="right" nowrap>&nbsp;</td>
      <td colspan="2"><input type="submit" value="Insert record">
      </td>
    </tr>
  </table>
  <input type="hidden" name="USER_ID" value="" size="32">
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>

